﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.Administration;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.UI.Models;
using MethodWorx.Core.DomainModel;

namespace MethodWorx.CMS.UI.Controllers
{
    public class SystemController : Controller
    {
        private PhysicalDatabaseService dbService;
        private IApplicationSettingsRepository settings;
        private IUnitOfWorkFactory unitOfWorkFactory;
        //
        // GET: /System/
        public SystemController(IUnitOfWorkFactory unitOfWorkFactory, IApplicationSettingsRepository settings, PhysicalDatabaseService dbService)
        {
            this.dbService = dbService;
            this.unitOfWorkFactory = unitOfWorkFactory;
            this.settings = settings;
        }

        [HttpGet]
        public ActionResult ApplicationSettings(bool ? fromContent)
        {
            var appSettings = this.settings.FindSettings();
            if (appSettings != null)
            {
                return View(new ApplicationSettingsModel()
                {
                    FromContent = fromContent ?? false,
                    ApplicationTypeId = appSettings.ApplicationTypeId,
                    BaseUri = appSettings.BaseUri
                });
            }
            else
            {
                return View(new ApplicationSettingsModel()
                                {
                                    FromContent = fromContent ?? false
                                });
            }
        }

        [HttpPost]
        public ActionResult ApplicationSettings(ApplicationSettingsModel applicationSettings, bool? fromContent)
        {
            if (applicationSettings == null)
                throw new ArgumentNullException("applicationSettings");

            if (ModelState.IsValid)
            {
                using (var uow = this.unitOfWorkFactory.Begin())
                {
                    ApplicationSettings settings = this.settings.FindSettings();
                    bool created = false;

                    if (settings == null)
                    {
                        settings = this.settings.CreateEntityInstance(); ;
                        created = true;
                        uow.RegisterAdded(settings);
                    }
                    else
                        uow.RegisterChanged(settings);

                    //
                    //  ok, we have the settings
                    //  lets save
                    settings.BaseUri = applicationSettings.BaseUri;
                    settings.ApplicationTypeId = applicationSettings.ApplicationTypeId.Value;


                    uow.Commit();
                }
                if (fromContent ?? false)
                    return Redirect("~/Content/Index");
                else
                    return RedirectToAction("Index", "Administration");
            }
            else
                return View(new ApplicationSettingsModel()
                                {
                                    FromContent = fromContent ?? false
                                });
        }

        public ActionResult ClearDatabase()
        {
            //
            //  ok, this is a destructive operation
            //  we are going to use
            try
            {
                this.dbService.ClearDatabase();
                return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(new { Success = false, ErrorMessage = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }

    }
}
